//
//  GXPersistent.m
//  GXSign
//
//  Created by 蓝其 on 16/11/15.
//  Copyright © 2016年 GX. All rights reserved.
//

#import "RBPersistence.h"
#import <YYCategories/NSString+YYAdd.h>
#import <YYCache.h>
#import <NSArray+YYAdd.h>

//版本号
static NSString *const RB_V_K = @"APP_VERSION_KEY";

@implementation RBPersistence

#pragma mark - 版本号

+ (NSString *)versionKey
{
    return [RB_V_K sha1String];
}

+ (void)setStoredVersion: (NSString *)version
{
    [self storeValue:version withKey:[self versionKey]];
}
+ (NSString *)getStoredVersion
{
    return [self getValueWithKey:[self versionKey]];
}

+ (NSString *)getProjectVersion
{
    return [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
}

#pragma mark - 路径

+ (NSString *)cachePathWithName: (NSString *)name
{
    NSString *cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSString *path = [cacheFolder stringByAppendingPathComponent:name];
    return path;
}

#pragma mark - 硬盘存储单例

+ (YYDiskCache *)diskInstance
{
    static YYDiskCache *cache = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cache = [[YYDiskCache alloc] initWithPath:[self cachePathWithName:@"uiofdas213"]];
    });
    return cache;
}

#pragma mark - 存取

+ (void)storeValue: (id)value withKey: (NSString *)key
{
    [[self diskInstance] setObject:value forKey:key];
}

+ (id)getValueWithKey: (NSString *)key
{
    return [[self diskInstance] objectForKey:key];
}

#pragma mark - 搜索历史
static NSString *const RB_S_S_H_K = @"my_show_search_history_key";

+ (NSString *)showSearchHistoryKeyWithUserId: (NSString *)userId type: (NSInteger)type
{
    if (!userId)
    {
        userId = @"";
    }
    return [[RB_S_S_H_K stringByAppendingString:userId] stringByAppendingString:[NSString stringWithFormat:@"%zd", type]];
}

+ (void)addShowSearchHistoryWithItem: (NSString *)item userId: (NSString *)userId type: (NSInteger)type
{
    YYDiskCache *cache = [self diskInstance];
    NSArray *cacheArray = [self getShowSearchHistoryWithUserId:userId type:type];
    NSMutableArray *items = [NSMutableArray arrayWithArray:cacheArray? cacheArray: @[]];
    [items removeObject:item];
    [items prependObject:item];
    [cache setObject:items forKey:[self showSearchHistoryKeyWithUserId:userId type:type]];
}

+ (void)deleteShowSearchHistoryWithItem: (NSString *)item userId: (NSString *)userId type: (NSInteger)type
{
    YYDiskCache *cache = [self diskInstance];
    NSArray *cacheArray = [self getShowSearchHistoryWithUserId:userId type:type];
    NSMutableArray *items = [NSMutableArray arrayWithArray:cacheArray? cacheArray: @[]];
    [items removeObject:item];
    [cache setObject:items forKey:[self showSearchHistoryKeyWithUserId:userId type:type]];
}

+ (void)deleteShowSearchHistoryWithIndex: (NSInteger)index userId: (NSString *)userId type: (NSInteger)type
{
    YYDiskCache *cache = [self diskInstance];
    NSArray *cacheArray = [self getShowSearchHistoryWithUserId:userId type:type];
    NSMutableArray *items = [NSMutableArray arrayWithArray:cacheArray? cacheArray: @[]];
    [items removeObjectAtIndex:index];
    [cache setObject:items forKey:[self showSearchHistoryKeyWithUserId:userId type:type]];
}

+ (void)clearShowSearchHistoryWithUserId: (NSString *)userId type: (NSInteger)type
{
    YYDiskCache *cache = [self diskInstance];
    [cache setObject:nil forKey:[self showSearchHistoryKeyWithUserId:userId type:type]];
}

+ (NSArray *)getShowSearchHistoryWithUserId: (NSString *)userId type: (NSInteger)type
{
    return (NSArray *)[[self diskInstance] objectForKey:[self showSearchHistoryKeyWithUserId:userId type:type]];
}


@end
